package ua.edu.nuos.jeetraining2012.cms.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

/**
 * CourseMaterialEntity.
 * <p/>
 * Class that describes course materials chapters.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 18.05.13 Time: 16:25
 * @since 1.0
 */
@Entity
@Table(name = "materials")
@SqlResultSetMappings({
        @SqlResultSetMapping(name="MaterialsTitlesReMapping", columns = {
                @ColumnResult(name = "id"),
                @ColumnResult(name = "section_title"),
                @ColumnResult(name = "title")
        }),
        @SqlResultSetMapping(name = "MaterialInfoMapping", entities={
                @EntityResult(entityClass=CourseEntity.class, fields={
                        @FieldResult(name="syllabusLink", column = "syllabusLink"),
                        @FieldResult(name="courseId", column = "id"),
                        @FieldResult(name="courseTitle", column="title"),
                        @FieldResult(name="courseStartDate", column="startDate"),
                        @FieldResult(name="courseFinishDate", column="finishDate")
                })
        })
})
public class CourseMaterialEntity implements Serializable {

    /**
     * Id.
     */
    @Id
    @SequenceGenerator(
            name = "materials_id_seq",
            sequenceName = "materials_id_seq",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "materials_id_seq"
    )
    @Column(
            name = "id",
            nullable = false,
            insertable = true,
            updatable = false,
            length = 11,
            precision = 0,
            columnDefinition = "SERIAL"
    )
    @NotNull
    private long id;

    /**
     * Section title.
     * E.g. week 1 or sth similar.
     */
    @Column(name = "section_title", nullable = false, length = 512)
    @Basic(optional = false)
    @Size(max = 512)
    private String sectionTitle;

    /**
     * Material title.
     * I.e. week 1 -- Introduction.
     */
    @Column(name = "title", nullable = false)
    @Basic(optional = false)
    @NotNull
    private String title;

    /**
     * Short description.
     */
    @Column(name = "short_description", nullable = false, columnDefinition = "TEXT")
    @Basic(optional = false)
    @NotNull
    private String shortDescription;

    /**
     * Material text part.
     */
    @Column(name = "text", nullable = true, columnDefinition = "TEXT")
    @Basic(optional = true)
    private String text;

    /**
     * URL to video file.
     */
    @Column(name = "video_url", nullable = true)
    @Basic(optional = true)
    private String videoURL;

    /**
     * URL to slides.
     */
    @Column(name = "slide_url", nullable = true)
    @Basic(optional = true)
    private String slideURL;

    /**
     * Course available since date.
     */
    @Column(name = "start_date", nullable = true)
    @Basic(optional = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date startDate;

    /**
     * Course available due date.
     */
    @Column(name = "due_date", nullable = true)
    @Basic(optional = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date dueDate;

    /** Define Google calendar event ID. */
    @Column(name = "calendar_event_id")
    @Basic(optional = true)
    private String eventId;
    /**
     * Quiz.
     */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "quiz_to_materials",
            joinColumns = @JoinColumn(name = "material_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "quiz_id", referencedColumnName = "id"))
    private Collection<ua.edu.nuos.jeetraining2012.cms.entity.QuizEntity> quiz;

    /**
     * Courses.
     */
    @ManyToOne
    private CourseEntity course;


    // Default constructor
    public CourseMaterialEntity() {}

    // Getters & Setters

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSectionTitle() {
        return sectionTitle;
    }

    public void setSectionTitle(String sectionTitle) {
        this.sectionTitle = sectionTitle;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getVideoURL() {
        return videoURL;
    }

    public void setVideoURL(String videoURL) {
        this.videoURL = videoURL;
    }

    public String getSlideURL() {
        return slideURL;
    }

    public void setSlideURL(String slideURL) {
        this.slideURL = slideURL;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public Collection<ua.edu.nuos.jeetraining2012.cms.entity.QuizEntity> getQuiz() {
        return quiz;
    }

    public void setQuiz(Collection<ua.edu.nuos.jeetraining2012.cms.entity.QuizEntity> quiz) {
        this.quiz = quiz;
    }

    public CourseEntity getCourse() {
        return course;
    }

    public void setCourse(CourseEntity course) {
        this.course = course;
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    // Overrides
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CourseMaterialEntity that = (CourseMaterialEntity) o;

        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        return "CourseMaterialEntity{" +
                "id=" + id +
                ", sectionTitle='" + sectionTitle + '\'' +
                ", title='" + title + '\'' +
                ", shortDescription='" + shortDescription + '\'' +
                ", text='" + text + '\'' +
                ", videoURL='" + videoURL + '\'' +
                ", slideURL='" + slideURL + '\'' +
                ", startDate=" + startDate +
                ", dueDate=" + dueDate +
                ", quiz=" + quiz +
                '}';
    }
}
